package com.lvtulife.common.web.util;

import com.lvtulife.common.util.StringUtil;
import com.lvtulife.common.web.util.search.SearchParams;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class RequestUtil {

    /**
     * 获取分页信息
     *
     * @param request
     * @return
     */
    public static RequestPageInfo getRequestPageInfo(HttpServletRequest request) {
        RequestPageInfo info = new RequestPageInfo(1, 15);
        if (request != null) {
            String page = request.getParameter("page");
            if (StringUtil.isNumeric(page)) {
                info.setPage(Integer.parseInt(page));
            }

            String rowsPerPage = StringUtil.isNumeric(request.getParameter("rowsPerPage")) ? request.getParameter("rowsPerPage") : request.getParameter("rows");
            if (StringUtil.isNumeric(rowsPerPage)) {
                info.setRowsPerPage(Integer.parseInt(rowsPerPage));
            }
        }
        return info;
    }

    public static SearchParams getSearchParams(HttpServletRequest request, String configName) {
        SearchParams sp = new SearchParams(configName);
        // 获取查询参数 取出请求中参数值不为null,且参数名以“QUERY_”开头的参数
        Iterator entries = request.getParameterMap().entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry entry = (Map.Entry) entries.next();
            String name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (name.startsWith("QUERY_") && valueObj != null) {
                String value  = "";
                if (valueObj instanceof String[]) {
                    String[] values = (String[]) valueObj;
                    for (int i = 0; i < values.length; i++) {
                        value = values[i] + ",";
                    }
                    value = value.substring(0, value.length() - 1);
                } else {
                    value = valueObj.toString();
                }

                sp.addParam(name, value);
            }
        }

        // 获取排序参数
        if (request.getParameter("sort") != null && request.getParameter("order") != null) {
            String sorts[] = request.getParameter("sort").split(",");
            String orders[] = request.getParameter("order").split(",");
            for (int i = 0; i < sorts.length; i++) {
                if (!StringUtils.isBlank(sorts[i]) && !StringUtils.isBlank(orders[i])) {
                    if ("desc".equals(orders[i].toLowerCase())) {
                        sp.addOrderDesc(sorts[i]);
                    } else {
                        sp.addOrderAsc(sorts[i]);
                    }
                } else if (!StringUtils.isBlank(sorts[i])) {
                    sp.addOrderAsc(sorts[i]);
                }
            }
        }

        return sp;
    }


    /**
     * 从request中获得参数Map，并返回可读的Map
     *
     * @param request
     * @return
     */
    public static Map getParameterMap(HttpServletRequest request) {
        // 返回值Map
        Map returnMap = new HashMap();
        // 参数Map
        Iterator entries = request.getParameterMap().entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        return returnMap;
    }
}
